home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / movelight.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  7.4 KB  |  290 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /**
  9.  * (c) Copyright 1993, Silicon Graphics, Inc.
  10.  * ALL RIGHTS RESERVED 
  11.  * Permission to use, copy, modify, and distribute this software for 
  12.  * any purpose and without fee is hereby granted, provided that the above
  13.  * copyright notice appear in all copies and that both the copyright notice
  14.  * and this permission notice appear in supporting documentation, and that 
  15.  * the name of Silicon Graphics, Inc. not be used in advertising
  16.  * or publicity pertaining to distribution of the software without specific,
  17.  * written prior permission. 
  18.  *
  19.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  20.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  21.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  22.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  23.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  24.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  25.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  26.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  27.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  28.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  29.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  30.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  31.  * 
  32.  * US Government Users Restricted Rights 
  33.  * Use, duplication, or disclosure by the Government is subject to
  34.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  35.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  36.  * clause at DFARS 252.227-7013 and/or in similar or successor
  37.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  38.  * Unpublished-- rights reserved under the copyright laws of the
  39.  * United States.  Contractor/manufacturer is Silicon Graphics,
  40.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  41.  *
  42.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  43.  */
  44. /**
  45.  *  movelight.c
  46.  *  This program demonstrates when to issue lighting and 
  47.  *  transformation commands to render a model with a light 
  48.  *  which is moved by a modeling transformation (rotate or 
  49.  *  translate).  The light position is reset after the modeling 
  50.  *  transformation is called.  The eye position does not change.
  51.  *
  52.  *  A sphere is drawn using a grey material characteristic. 
  53.  *  A single light source illuminates the object.
  54.  *
  55.  *  Interaction:  pressing the left or middle mouse button
  56.  *  alters the modeling transformation (x rotation) by 30 degrees.  
  57.  *  The scene is then redrawn with the light in a new position.
  58.  */
  59. #include <stdlib.h>
  60. #include <stdarg.h>
  61. #include <stdio.h>
  62. #include <GL/glut.h>
  63.  
  64. #define TORUS 0
  65. #define TEAPOT 1
  66. #define DOD 2
  67. #define TET 3
  68. #define ISO 4
  69. #define QUIT 5
  70.  
  71. static int spin = 0;
  72. static int obj = TORUS;
  73. static int begin;
  74.  
  75. void
  76. output(GLfloat x, GLfloat y, char *format,...)
  77. {
  78.   va_list args;
  79.   char buffer[200], *p;
  80.  
  81.   va_start(args, format);
  82.   vsprintf(buffer, format, args);
  83.   va_end(args);
  84.   glPushMatrix();
  85.   glTranslatef(x, y, 0);
  86.   for (p = buffer; *p; p++)
  87.     glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  88.   glPopMatrix();
  89. }
  90.  
  91. void
  92. menu_select(int item)
  93. {
  94.   if (item == QUIT)
  95.     exit(0);
  96.   obj = item;
  97.   glutPostRedisplay();
  98. }
  99.  
  100. /* ARGSUSED2 */
  101. void
  102. movelight(int button, int state, int x, int y)
  103. {
  104.   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  105.     begin = x;
  106.   }
  107. }
  108.  
  109. /* ARGSUSED1 */
  110. void
  111. motion(int x, int y)
  112. {
  113.   spin = (spin + (x - begin)) % 360;
  114.   begin = x;
  115.   glutPostRedisplay();
  116. }
  117.  
  118. void
  119. myinit(void)
  120. {
  121.   glEnable(GL_LIGHTING);
  122.   glEnable(GL_LIGHT0);
  123.  
  124.   glDepthFunc(GL_LESS);
  125.   glEnable(GL_DEPTH_TEST);
  126. }
  127.  
  128. /*  Here is where the light position is reset after the modeling
  129.  *  transformation (glRotated) is called.  This places the 
  130.  *  light at a new position in world coordinates.  The cube
  131.  *  represents the position of the light.
  132.  */
  133. void
  134. display(void)
  135. {
  136.   GLfloat position[] =
  137.   {0.0, 0.0, 1.5, 1.0};
  138.  
  139.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  140.   glMatrixMode(GL_MODELVIEW);
  141.   glPushMatrix();
  142.   glTranslatef(0.0, 0.0, -5.0);
  143.  
  144.   glPushMatrix();
  145.   glRotated((GLdouble) spin, 0.0, 1.0, 0.0);
  146.   glRotated(0.0, 1.0, 0.0, 0.0);
  147.   glLightfv(GL_LIGHT0, GL_POSITION, position);
  148.  
  149.   glTranslated(0.0, 0.0, 1.5);
  150.   glDisable(GL_LIGHTING);
  151.   glColor3f(0.0, 1.0, 1.0);
  152.   glutWireCube(0.1);
  153.   glEnable(GL_LIGHTING);
  154.   glPopMatrix();
  155.  
  156.   switch (obj) {
  157.   case TORUS:
  158.     glutSolidTorus(0.275, 0.85, 20, 20);
  159.     break;
  160.   case TEAPOT:
  161.     glutSolidTeapot(1.0);
  162.     break;
  163.   case DOD:
  164.     glPushMatrix();
  165.     glScalef(.5, .5, .5);
  166.     glutSolidDodecahedron();
  167.     glPopMatrix();
  168.     break;
  169.   case TET:
  170.     glutSolidTetrahedron();
  171.     break;
  172.   case ISO:
  173.     glutSolidIcosahedron();
  174.     break;
  175.   }
  176.  
  177.   glPopMatrix();
  178.   glPushAttrib(GL_ENABLE_BIT);
  179.   glDisable(GL_DEPTH_TEST);
  180.   glDisable(GL_LIGHTING);
  181.   glMatrixMode(GL_PROJECTION);
  182.   glPushMatrix();
  183.   glLoadIdentity();
  184.   gluOrtho2D(0, 3000, 0, 3000);
  185.   glMatrixMode(GL_MODELVIEW);
  186.   glPushMatrix();
  187.   glLoadIdentity();
  188.   output(80, 2800, "Welcome to movelight.");
  189.   output(80, 2650, "Right mouse button for menu.");
  190.   output(80, 400, "Hold down the left mouse button");
  191.   output(80, 250, "and move the mouse horizontally");
  192.   output(80, 100, "to change the light position.");
  193.   glPopMatrix();
  194.   glMatrixMode(GL_PROJECTION);
  195.   glPopMatrix();
  196.   glPopAttrib();
  197.   glutSwapBuffers();
  198. }
  199.  
  200. void
  201. myReshape(int w, int h)
  202. {
  203.   glViewport(0, 0, w, h);
  204.   glMatrixMode(GL_PROJECTION);
  205.   glLoadIdentity();
  206.   gluPerspective(40.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
  207.   glMatrixMode(GL_MODELVIEW);
  208. }
  209.  
  210. void
  211. tmotion(int x, int y)
  212. {
  213.   printf("Tablet motion x = %d, y = %d\n", x, y);
  214. }
  215.  
  216. void
  217. tbutton(int b, int s, int x, int y)
  218. {
  219.   printf("b = %d, s = %d, x = %d, y = %d\n", b, s, x, y);
  220. }
  221.  
  222. void
  223. smotion(int x, int y, int z)
  224. {
  225.   fprintf(stderr, "Spaceball motion %d %d %d\n", x, y, z);
  226. }
  227.  
  228. void
  229. rmotion(int x, int y, int z)
  230. {
  231.   fprintf(stderr, "Spaceball rotate %d %d %d\n", x, y, z);
  232. }
  233.  
  234. void
  235. sbutton(int button, int state)
  236. {
  237.   fprintf(stderr, "Spaceball button %d is %s\n",
  238.     button, state == GLUT_UP ? "up" : "down");
  239. }
  240.  
  241. void
  242. dials(int dial, int value)
  243. {
  244.   fprintf(stderr, "Dial %d is %d\n", dial, value);
  245.   spin = value % 360;
  246.   glutPostRedisplay();
  247. }
  248.  
  249. void
  250. buttons(int button, int state)
  251. {
  252.   fprintf(stderr, "Button %d is %s\n", button,
  253.     state == GLUT_UP ? "up" : "down");
  254. }
  255.  
  256. /*  Main Loop
  257.  *  Open window with initial window size, title bar, 
  258.  *  RGBA display mode, and handle input events.
  259.  */
  260. int
  261. main(int argc, char **argv)
  262. {
  263.   glutInit(&argc, argv);
  264.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  265.   glutInitWindowSize(500, 500);
  266.   glutCreateWindow(argv[0]);
  267.   myinit();
  268.   glutMouseFunc(movelight);
  269.   glutMotionFunc(motion);
  270.   glutReshapeFunc(myReshape);
  271.   glutDisplayFunc(display);
  272.   glutTabletMotionFunc(tmotion);
  273.   glutTabletButtonFunc(tbutton);
  274.   glutSpaceballMotionFunc(smotion);
  275.   glutSpaceballRotateFunc(rmotion);
  276.   glutSpaceballButtonFunc(sbutton);
  277.   glutDialsFunc(dials);
  278.   glutButtonBoxFunc(buttons);
  279.   glutCreateMenu(menu_select);
  280.   glutAddMenuEntry("Torus", TORUS);
  281.   glutAddMenuEntry("Teapot", TEAPOT);
  282.   glutAddMenuEntry("Dodecahedron", DOD);
  283.   glutAddMenuEntry("Tetrahedron", TET);
  284.   glutAddMenuEntry("Icosahedron", ISO);
  285.   glutAddMenuEntry("Quit", QUIT);
  286.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  287.   glutMainLoop();
  288.   return 0;             /* ANSI C requires main to return int. */
  289. }
  290.